package com.atguigu.tingshu.album.receiver;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.qcloud.vod.common.StringUtil;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



/**
 * ClassName: AlbumReceiver
 * Package: com.atguigu.tingshu.album.receiver
 * Description:
 *
 * @Author ycy
 * @Create 2024/9/23 20:53
 * @Version 1.0
 */
@Slf4j
@Component
public class AlbumReceiver {
    @Autowired
    private AlbumInfoService albumInfoService;

    @Autowired
    private TrackInfoService trackInfoService;

    /**
     * 监听到更新声音统计信息
     * @param msg
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TRACK,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_TRACK_STAT_UPDATE,durable = "true"),
            key = {MqConst.ROUTING_TRACK_STAT_UPDATE}
        )
    )
    public void updateTrackStat(String msg, Message message, Channel channel){
        if (StringUtil.isNotEmpty(msg)) {

            TrackStatMqVo mqVo = JSON.parseObject(msg,TrackStatMqVo.class);

            trackInfoService.updateTrackStat(mqVo);

        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
